package com.star.bookstore.product.controller;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.star.bookstore.product.entity.CategoryEntity;
import com.star.bookstore.product.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.star.bookstore.product.entity.AttrEntity;
import com.star.bookstore.product.service.AttrService;
import com.star.bookstore.common.utils.PageUtils;
import com.star.bookstore.common.utils.R;

import javax.validation.Valid;


/**
 * 书本的属性表
 *
 * @author wuLiFei
 * @email wuLiFei.@gmail.com
 * @date 2021-03-07 11:04:13
 */
@RestController
@RequestMapping("product/attr")
@Slf4j
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){

		AttrEntity attr = attrService.getById(attrId);

		//获取分类所属数组
        Long[] categoryIds = categoryService.getAttrCategoryPath(attr);
        attr.setCategoryPath(categoryIds);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Valid @RequestBody AttrEntity attr){

        //获取分类父id，书名插入相应的分类表数据
        Long categoryPId = attr.getCategoryPId();
        String bookName = attr.getBookName();
        log.info("父id： " + categoryPId);
        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setName(bookName);
        categoryEntity.setParentId(categoryPId);
        categoryEntity.setBookLevel(3);
        categoryEntity.setShowStatus(1);
        categoryService.save(categoryEntity);

        //上架时间为当前系统时间
        attr.setShelveDate(new Timestamp(new Date().getTime()));
        //分类Id为分类表的书本id
        attr.setCategoryId(categoryEntity.getBookId());
        //所属父分类名称
        attr.setCategoryName(categoryService.getById(categoryPId).getName());
        attr.setShowStatus(1);
        attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@Valid @RequestBody AttrEntity attr){

        //获取分类父id，书名更新相应的分类表数据
        Long categoryPId = attr.getCategoryPId();
        String bookName = attr.getBookName();
        //获取需要修改的属性
        CategoryEntity categoryEntity = categoryService.getById(attr.getCategoryId());
        categoryEntity.setParentId(categoryPId);
        categoryEntity.setName(bookName);
        categoryService.updateById(categoryEntity);

        //更新所属父分类名称
        attr.setCategoryName(categoryService.getById(categoryPId).getName());
        attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){


        //删除关联的分类表
		for (int i = 0; i < attrIds.length; i++){
            Long categoryId = attrService.getById(attrIds[i]).getCategoryId();
            categoryService.removeById(categoryId);
        }

        attrService.removeByIds(Arrays.asList(attrIds));


        return R.ok();
    }

}
